package com.next

import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured
import grails.validation.ValidationException
import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR'])
@Transactional
class ContactProfessionTitleController
{

    ContactProfessionTitleService contactProfessionTitleService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond contactProfessionTitleService.list(params), model: [contactProfessionTitleCount: contactProfessionTitleService.count()]
    }

    def show(Long id)
    {
        respond contactProfessionTitleService.get(id)
    }

    def create()
    {
        respond new ContactProfessionTitle(params)
    }

    def save(ContactProfessionTitle contactProfessionTitle)
    {
        if (contactProfessionTitle == null)
        {
            notFound()
            return
        }

        try
        {
            contactProfessionTitleService.save(contactProfessionTitle)
        }
        catch (ValidationException e)
        {
            respond contactProfessionTitle.errors, view: 'create'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'contactProfessionTitle.label', default: 'ContactProfessionTitle'), contactProfessionTitle.id])
                redirect contactProfessionTitle
            }
            '*' { respond contactProfessionTitle, [status: CREATED] }
        }
    }

    def edit(Long id)
    {
        respond contactProfessionTitleService.get(id)
    }

    def update(ContactProfessionTitle contactProfessionTitle)
    {
        if (contactProfessionTitle == null)
        {
            notFound()
            return
        }

        try
        {
            contactProfessionTitleService.save(contactProfessionTitle)
        }
        catch (ValidationException e)
        {
            respond contactProfessionTitle.errors, view: 'edit'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'contactProfessionTitle.label', default: 'ContactProfessionTitle'), contactProfessionTitle.id])
                redirect contactProfessionTitle
            }
            '*' { respond contactProfessionTitle, [status: OK] }
        }
    }

    def delete(Long id)
    {
        if (id == null)
        {
            notFound()
            return
        }

        contactProfessionTitleService.delete(id)

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'contactProfessionTitle.label', default: 'ContactProfessionTitle'), id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'contactProfessionTitle.label', default: 'ContactProfessionTitle'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
